﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeiBOAPI;
using WeiBoUI.FormEx;

namespace KKWeiBO
{
    public partial class RegFrm : FormBase
    {
        public RegFrm()
        {
            InitializeComponent();

            Global.Instance._g_SinaService._ac_RequestToken += new Action<string>(RequestTokenImpl);
            Global.Instance._g_SinaService._ac_GetToken += new Action<string>(GetTokenImpl);
        }

        public void RequestTokenImpl(string url)
        {
            this.BeginInvoke((MethodInvoker)delegate { this.webBrowser1.Navigate(url); });
            
            
        }

        public void GetTokenImpl(string url)
        {
            this.BeginInvoke((MethodInvoker)delegate { this.Close(); });

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }

        private void kButton2_Click(object sender, EventArgs e)
        {
            Global.Instance._g_SinaService._loginName = txtUserName.Text;
            Global.Instance._g_SinaService._loginPwd = txtUserPwd.Text;
            Global.Instance._g_SinaService.GetAccessToken(this.txtVerfer.Text);
        }

        private void kButton1_Click(object sender, EventArgs e)
        {
            Global.Instance._g_SinaService.GetRequestToken();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            setForm("userId", "value", txtUserName.Text);
            setForm("passwd", "value", txtUserPwd.Text);
            setForm("regagreementCheckbox", "checked", "checked");
        }

        public void setForm(string documemtID, string attrName, string value)
        {
            HtmlElement htmlElement = webBrowser1.Document.All[documemtID];
            if (htmlElement != null)
            {
                htmlElement.SetAttribute(attrName, value);
            }
            
        }


    }
}
